package com.endoc.agingtools.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.endoc.agingtools.FinalStaticString;
import com.endoc.agingtools.ItemBean;
import com.endoc.agingtools.R;
import com.endoc.agingtools.litepal.BindDevice;
import com.endoc.agingtools.utils.BleUtils;
import com.endoc.agingtools.view.PopupWindows;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import cn.com.heaton.blelibrary.ble.Ble;
import cn.com.heaton.blelibrary.ble.model.BleDevice;

public class DeviceRecycleViewAdapter extends RecyclerView.Adapter<DeviceRecycleViewAdapter.MyViewHolder>{
    String TAG = "DeviceRecycleViewAdapter";
    private Context context;
    private List<BleDevice> list = new ArrayList<>();//搜到数据除了已经绑定的数据集合
    private List<BleDevice> originalDeviceList;//原始搜到所有的数据
    boolean isBind =false;//是否有绑定的值
    private View inflater;
    RecyclerView mRecyclerView;
    ItemBean mItemBean;
    CheckBox mCheckBox;
    RecyclerView.Adapter mKaweiAdapter;
    TextView mConnectStatus;
    //构造方法，传入数据
    public DeviceRecycleViewAdapter(Context context, ItemBean itemBean,List<BleDevice> list,RecyclerView recyclerView,CheckBox checkBox,RecyclerView.Adapter kaweiAdapter,TextView connectStatus){
        this.context = context;
        originalDeviceList =list;
        mRecyclerView =recyclerView;
        mItemBean = itemBean;
        mCheckBox =checkBox;
       mKaweiAdapter =kaweiAdapter;
        mConnectStatus =connectStatus;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder，返回每一项的布局
        inflater = LayoutInflater.from(context).inflate(R.layout.device_item,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(inflater);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.checkBox.setText(list.get(position).getBleName());
        holder.setButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

        /*        //将原先绑定设备从数据库中移除
                if(mItemBean.getDeviceName()!=null) {
                    BindDevice unbindDevice = new BindDevice();
                    unbindDevice.setUser(false);
                    unbindDevice.setKawei(mItemBean.getKaiwei());
                    unbindDevice.saveOrUpdate("deviceName = ?", mItemBean.getDeviceName());  //更改绑定设备为未绑定
                    //如果当前连接的设备列表中有此设备,那么就进行断开操作
                    List<BleDevice> connectedDevices = Ble.getInstance().getConnectedDevices();
                    for (int i = 0; i < connectedDevices.size(); i++) {
                        if (connectedDevices.get(i).getBleName().equals(mItemBean.getDeviceName())) {
                            Ble.getInstance().disconnect(connectedDevices.get(i));
                        }
                    }
                }*/

                //进行连接操作
                BleUtils.connectDevice(list.get(position),mKaweiAdapter,DeviceRecycleViewAdapter.this,mConnectStatus);
                //设置当前的设备为绑定设备
                BindDevice bindDevice =new BindDevice();
                bindDevice.setDeviceName(list.get(position).getBleName());//设置当前设备的名字
                bindDevice.setUser(true);
                bindDevice.setKawei(mItemBean.getKaiwei());
                bindDevice.saveOrUpdate("deviceName = ?", list.get(position).getBleName());  //保存所有
                mCheckBox.setVisibility(View.VISIBLE);
                mCheckBox.setChecked(true);

                mItemBean.setDeviceName(list.get(position).getBleName());


                //更新自己的ui
                notifyDataSetChanged();
                //更新主界面的ui
                mKaweiAdapter.notifyDataSetChanged();
                //mRecyclerView.setVisibility(View.INVISIBLE);
            }
        });
    }





    @Override
    public int getItemCount() {
        //查找当前卡位上是否有绑定的设备
        //BindDevice bindDevice = LitePal.where("isUser = ? and kawei = ?", "1", "" + mItemBean.getKaiwei()).findFirst(BindDevice.class);
        List<BindDevice> bindDevices = LitePal.where("isUser = ?", "1").find(BindDevice.class);
        list.clear();
        //添加集合数据
        list.addAll(originalDeviceList);
        for(int i=0;i<bindDevices.size();i++){
            //如果存在已经被其他卡位绑定的设备,那么就不显示了
            for(int j=0;j<list.size();j++){
                //如果存在相同的数据
                if(bindDevices.get(i).getDeviceName().equals(list.get(j).getBleName())){
                    //那么从列表中移除
                    list.remove(list.get(j));
                }
            }
        }
        return list.size();
    }

    //内部类，绑定控件
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView checkBox;
         TextView tvDeviceStatus;
         Button setButton;
         public MyViewHolder(View itemView) {
            super(itemView);
            checkBox = (TextView) itemView.findViewById(R.id.tv_device_select);
            tvDeviceStatus = (TextView) itemView.findViewById(R.id.tv_device_status);
            setButton = (Button) itemView.findViewById(R.id.tv_device_set);
        }
    }
}

